package com.rj1101.hscrm.demo.dao;

import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rj1101.hscrm.demo.model.Cinfo;
import com.rj1101.hscrm.demo.util.Paging;

/**
 * A data access object (DAO) providing persistence and search support for Cinfo
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.rj1101.hscrm.demo.model.Cinfo
 * @author MyEclipse Persistence Tools
 */

public class CinfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(CinfoDAO.class);
	// property constants
	public static final String CNAME = "cname";
	public static final String CARD_ID = "cardId";
	public static final String CGENDER = "cgender";
	public static final String CAGE = "cage";
	public static final String IS_MARRIED = "isMarried";
	public static final String OCCUPATIONAL = "occupational";
	public static final String BLOOD_GROUP = "bloodGroup";
	public static final String EDUCATION = "education";
	public static final String CADDR = "caddr";
	public static final String COMPANY = "company";
	public static final String RH_NEGATIVE = "rhNegative";
	public static final String HOME_TOWN = "homeTown";
	public static final String FROM_SRC = "fromSrc";
	public static final String MEDICAL_INSURANCE = "medicalInsurance";
	public static final String HABITS = "habits";
	public static final String DISABILITY_ID = "disabilityId";
	public static final String IS_DISABILITY = "isDisability";

	protected void initDao() {
		// do nothing
	}

	public void save(Cinfo transientInstance) {
		log.debug("saving Cinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Cinfo persistentInstance) {
		log.debug("deleting Cinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Cinfo findById(java.lang.Integer id) {
		log.debug("getting Cinfo instance with id: " + id);
		try {
			Cinfo instance = (Cinfo) getHibernateTemplate().get(
					"com.rj1101.hscrm.demo.model.Cinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Cinfo instance) {
		log.debug("finding Cinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Cinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Cinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCname(Object cname) {
		return findByProperty(CNAME, cname);
	}

	public List findByCardId(Object cardId) {
		return findByProperty(CARD_ID, cardId);
	}

	public List findByCgender(Object cgender) {
		return findByProperty(CGENDER, cgender);
	}

	public List findByCage(Object cage) {
		return findByProperty(CAGE, cage);
	}

	public List findByIsMarried(Object isMarried) {
		return findByProperty(IS_MARRIED, isMarried);
	}

	public List findByOccupational(Object occupational) {
		return findByProperty(OCCUPATIONAL, occupational);
	}

	public List findByBloodGroup(Object bloodGroup) {
		return findByProperty(BLOOD_GROUP, bloodGroup);
	}

	public List findByEducation(Object education) {
		return findByProperty(EDUCATION, education);
	}

	public List findByCaddr(Object caddr) {
		return findByProperty(CADDR, caddr);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findByRhNegative(Object rhNegative) {
		return findByProperty(RH_NEGATIVE, rhNegative);
	}

	public List findByHomeTown(Object homeTown) {
		return findByProperty(HOME_TOWN, homeTown);
	}

	public List findByFromSrc(Object fromSrc) {
		return findByProperty(FROM_SRC, fromSrc);
	}

	public List findByMedicalInsurance(Object medicalInsurance) {
		return findByProperty(MEDICAL_INSURANCE, medicalInsurance);
	}

	public List findByHabits(Object habits) {
		return findByProperty(HABITS, habits);
	}

	public List findByDisabilityId(Object disabilityId) {
		return findByProperty(DISABILITY_ID, disabilityId);
	}

	public List findByIsDisability(Object isDisability) {
		return findByProperty(IS_DISABILITY, isDisability);
	}

	public List findAll() {
		log.debug("finding all Cinfo instances");
		try {
			String queryString = "from Cinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Cinfo merge(Cinfo detachedInstance) {
		log.debug("merging Cinfo instance");
		try {
			Cinfo result = (Cinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Cinfo instance) {
		log.debug("attaching dirty Cinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Cinfo instance) {
		log.debug("attaching clean Cinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public List findCustomerInfoByPage(String tbName,String tbGetFields,int pageSize, int pageIndex,
			String tbFileds,String orderDirection){
		log.debug("finding all Cinfo instances");
		try {
			
			SessionFactory sf = getHibernateTemplate().getSessionFactory();
			Session session = sf.openSession();
			Transaction tx =session.beginTransaction();
			SQLQuery query =session.createSQLQuery("{call PagerSelect(?,?,?,?,?,?)}").addEntity(Cinfo.class);
			query.setString(0,tbName);
			query.setString(1, tbGetFields);
			query.setInteger(2, pageSize);
			query.setInteger(3, pageIndex);
			query.setString(4, tbFileds);
			query.setString(5, orderDirection);
			tx.commit();
			
			return query.list();
		
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Cinfo> pagingCustomerInfo(String tableName, Paging pager){
		log.debug("find cinfo by paging...");
		try{
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			String sql="SELECT TOP "+pager.getNumPerPage()+" * FROM "+tableName+
					" WHERE "+pager.getOrderField()+" >= " +
					"(SELECT TOP 1 "+pager.getOrderField()+
					" FROM (SELECT TOP "+(pager.getNumPerPage()*(pager.getPageNum()-1)+1)+" "+
					pager.getOrderField()+" FROM "+tableName+" ORDER BY "+pager.getOrderField()+
					" ASC) AS TEMP " +
					"ORDER BY "+pager.getOrderField()+" DESC)";
			Query query = session.createSQLQuery(sql).addEntity(Cinfo.class);
			return query.list();
		}catch(RuntimeException re){
			log.error("find cinfo by paging failed.", re);
			throw re;
		}
	}
	public static CinfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CinfoDAO) ctx.getBean("CinfoDAO");
	}
	
}